package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecZmPaperManagement;
import com.ruoyi.system.domain.RecordType;
import com.ruoyi.system.service.IRecordTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 档案分类Controller
 *
 * @author admin
 * @date 2022-02-19
 */
@RestController
@RequestMapping("/system/type")
public class RecordTypeController extends BaseController {
    @Autowired
    private IRecordTypeService recordTypeService;

    /**
     * 查询档案分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public AjaxResult list(RecordType recordType) {
        List<RecordType> list = recordTypeService.selectRecordTypeList(recordType);
        return AjaxResult.success(list);
    }

    /**
     * 导出 档案分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "档案分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordType recordType) {
        List<RecordType> list = recordTypeService.selectRecordTypeList(recordType);
        ExcelUtil<RecordType> util = new ExcelUtil<RecordType>(RecordType.class);
        util.exportExcel(response, list, "档案分类数据");
    }

    /**
     * 导入模板 档案分类
     */
    @Log(title = "档案分类模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecordType> util = new ExcelUtil<RecordType>(RecordType.class);
        util.importTemplateExcel(response, "档案分类数据");
    }

    /**
     * 档案分类导入
     */
    @Log(title = "档案分类导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:type:importData')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecordType> util = new ExcelUtil<RecordType>(RecordType.class);
        List<RecordType> fileList = util.importExcel(file.getInputStream());
        String message = recordTypeService.importFile(fileList);
        return AjaxResult.success(message);
    }

    /**
     * 获取档案分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") String typeId) {
        return AjaxResult.success(recordTypeService.selectRecordTypeByTypeId(typeId));
    }

    /**
     * 获取建档标识档案
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/flag")
    public AjaxResult getFlagType(String typeId) {
        return AjaxResult.success(recordTypeService.selectFlagType(typeId));
    }

    /**
     * 新增档案分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "档案分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordType recordType) {
        return toAjax(recordTypeService.insertRecordType(recordType));
    }

    /**
     * 修改档案分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "档案分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordType recordType) {
        return toAjax(recordTypeService.updateRecordType(recordType));
    }

    /**
     * 删除档案分类
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "档案分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable String[] typeIds) {
        return toAjax(recordTypeService.deleteRecordTypeByTypeIds(typeIds));
    }

    /**
     * 获取分类下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(RecordType recordType) {
        List<RecordType> types = recordTypeService.selectRecordTypeList(recordType);
        return AjaxResult.success(recordTypeService.buildRecordTypeTreeSelect(types));
    }
}
